<template>
	<view>
			<view class='personal-data'>
				<view class="headPortrait">
					<view @click="changePhoto">
						<image :src="userInfo.avatar?userInfo.avatar:'/static/user/default.png'"></image>
					</view>
					<view style="color: #FF4B15;">
						点击更换头像
					</view>
				</view>
				<view class='list'>
					<view class='item acea-row row-between-wrapper'>
						<view>昵称</view>
						<view @click="setNickname" class='input acea-row row-between-wrapper'>
							<input type='text' name='nickname' :value='userInfo.nickname'></input>
							<text class='cuIcon-right' style="margin-right: 20rpx;"></text>
						</view>
					</view>
					
					<view class='item acea-row row-between-wrapper'>
						<view>ID号</view>
						<view class='input acea-row row-between-wrapper'>
							<input type='text' :value='userInfo.uid' disabled='true' class='id'></input>
							<text class='iconfont icon-suozi'></text>
						</view>
					</view>
					
					<!-- <view class='item acea-row row-between-wrapper'>
						<view>手机号码</view>
						<navigator url="/pages/users/user_phone/index" hover-class="none" class="input" v-if="!userInfo.phone">
							点击绑定手机号<text class="iconfont icon-xiangyou"></text>
						</navigator>
						<view class='input acea-row row-between-wrapper' v-else>
							<input type='text' disabled='true' name='phone' :value='userInfo.phone' class='id'></input>
							<text class='iconfont icon-suozi'></text>
						</view>
					</view> -->
					
					<view v-if="userInfo.spread_uid_name" class='item acea-row row-between-wrapper'>
						<view>上级推广人</view>
						<view class='input acea-row row-between-wrapper'>
							<input style="width: 400rpx;" type='text' :value='userInfo.spread_uid_name' disabled='true' class='id'></input>
							<text class='iconfont icon-suozi'></text>
						</view>
					</view>
					
					<!-- #ifndef H5 -->
					<view class='item acea-row row-between-wrapper'>
						<view>版本号</view>
						<view class='input acea-row row-between-wrapper'>
							<input type='text' :value='version_number' disabled='true' class='id'></input>
							<text class='iconfont icon-suozi'></text>
						</view>
					</view>
					<!-- #endif -->
					
					<!-- #ifdef MP -->
					<view class='item acea-row row-between-wrapper'>
						<view>权限设置</view>
						<view class="input" @click="Setting">
							点击管理<text class="iconfont icon-xiangyou"></text>
						</view>
					</view>
					<!-- #endif -->
<!-- 					<view class="item acea-row row-between-wrapper" v-if="userInfo.phone && userInfo.user_type == 'h5'">
						<view>密码</view>
						<navigator url="/pages/users/user_pwd_edit/index" hover-class="none" class="input">
							点击修改密码<text class="iconfont icon-xiangyou"></text>
						</navigator>
					</view> -->
				</view>
			<!-- 	<button class='modifyBnt bg-color' formType="submit">保存修改</button> -->

				<view class="logOut cart-color acea-row row-center-wrapper" @click="outLogin">退出登录</view>
				
				<uni-popup ref="popup" type="dialog">
					<uni-popup-dialog
						type="bottom"
						mode="input"
						:title="title"
						v-model.trim="newNickname"
						:placeholder="placeholder"
						:duration="2000"
						@close="close"
						@confirm="confirm"
					></uni-popup-dialog>
				</uni-popup>
			</view>
	</view>
</template>

<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	import uniPopupMessage from '@/components/uni-popup/uni-popup-message.vue';
	import uniPopupDialog from '@/components/uni-popup/uni-popup-dialog.vue';
	export default {
		data() {
			return {
				userInfo: {},
				loginType: 'h5', //app.globalData.loginType
				userIndex: 0,
				switchUserInfo: [],
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				url:'/static/images/headPortrait.png',
				version_number:'', // 版本号
				newNickname:'',
				placeholder: '请输入昵称',
				title: '昵称'
			};
		},
		components: {
			uniPopup,
			uniPopupMessage,
			uniPopupDialog
		},
		onLoad() {
			let that = this;
			if (this.$db.get('userToken')) {
				that.isLogin = true;
				// #ifndef H5
				this.version_number = plus.runtime.version
				// #endif
				
			}else{
				that.$common.jumpToLogin();
				return;
			}
			that.getUserInfo();
		},
		methods: {
			// 上传图片
			changePhoto() {
				this.$api.uploadImage(1, res => {
					if(res.status == 200) {
						this.$api.setAvatar({
							avatar:res.data.path
						},data => {
							if(data.status == 200) {
								this.userInfo.avatar = res.data.path
								this.$common.successToShow('更换成功');
							}else {
								this.$common.errorToShow('更换失败');
							}
						})
					}else {
						this.$common.errorToShow('更换失败');
					}
				})
			},
			setNickname() {
				this.$refs.popup.open(); //打开输入框
			},
			close(done) { 
				done();
			}, 
			confirm(done, nickname) {
				if(!nickname) {
					this.$common.errorToShow('请输入昵称');
					return
				}
				this.$api.setNickname({
					nickname:nickname
				},res => {
					if(res.status == 200) {
						this.userInfo.nickname = nickname
						this.$common.successToShow('修改成功');
					}else {
						this.$common.errorToShow('修改失败');
					}
				})
				done();
			},
			/**
			 * 获取个人用户信息
			 */
			getUserInfo: function() {
				let that = this;
				that.$api.getUserInfo(res=>{
					if(res.status==400){
						that.$common.errorToShow("请先登录");
						setTimeout(()=>{
							that.$common.navigateTo("/pages/login/login");
						},1000)
						return; 
					}
					that.userInfo = res.data
				})
			},	
			/**
			 * 退出登录
			 * 
			 */
			outLogin: function() {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '确认退出登录?',
					success: function (res) {
						if (res.confirm) {
							that.$api.getLogout({},res=>{
								that.$db.clear();
								that.$common.redirectTo("/pages/login/login");
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});	
			}
		}
	}
</script>

<style scoped lang="scss">
	page {
		background-color: #FFFFFF;
	}
	.personal-data .headPortrait {
		padding: 30rpx;
		text-align: center;
	}
	.personal-data .headPortrait view:first-child {
		width: 120rpx;
		height: 120rpx;
		border-radius: 50%;
		margin: 0 auto 10rpx;
		image {
			width: 100%;
			height: 100%;
			border-radius: 50%;
		}
	}
	.personal-data .wrapper {
		margin: 10rpx 0;
		background-color: #fff;
		padding: 36rpx 30rpx 13rpx 30rpx;
	}

	.personal-data .wrapper .title {
		margin-bottom: 30rpx;
		font-size: 32rpx;
		color: #282828;
	}

	.personal-data .wrapper .wrapList .item {
		width: 750rpx;
		height: 160rpx;
		background-color: #f8f8f8;
		border-radius: 20rpx;
		margin-bottom: 22rpx;
		position: relative;
		border: 2rpx solid #f8f8f8;
		box-sizing:border-box;
	}

	.personal-data .wrapper .wrapList .item .on {
		border-color: #e93323;
		border-radius: 20rpx;
		background-image: url("");
		background-size: 100% 100%;
		background-color: #fff9f9;
		background-repeat: no-repeat;
	}

	.personal-data .wrapper .wrapList .item .picTxt {
		width: 445rpx;
	}

	.personal-data .wrapper .wrapList .item .picTxt .pictrue {
		width: 96rpx;
		height: 96rpx;
		position: relative;
	}

	.personal-data .wrapper .wrapList .item .picTxt .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 50%;
	}

	.personal-data .wrapper .wrapList .item .picTxt .pictrue .alter {
		width: 30rpx;
		height: 30rpx;
		border-radius: 50%;
		position: absolute;
		bottom: 0;
		right: 0;
	}

	.personal-data .wrapper .wrapList .item .picTxt .text {
		width: 325rpx;
	}

	.personal-data .wrapper .wrapList .item .picTxt .text .name {
		width: 100%;
		font-size: 30rpx;
		color: #282828;
	}

	.personal-data .wrapper .wrapList .item .picTxt .text .phone {
		font-size: 24rpx;
		color: #999;
		margin-top: 10rpx;
	}

	.personal-data .wrapper .wrapList .item .bnt {
		font-size: 24rpx;
		background-color: #fff;
		border-radius: 27rpx;
		width: 140rpx;
		height: 54rpx;
		border: 2rpx solid #e93323;
	}

	.personal-data .wrapper .wrapList .item .currentBnt {
		position: absolute;
		right: 0;
		top: 0;
		font-size: 26rpx;
		background-color: rgba(233, 51, 35, 0.1);
		width: 140rpx;
		height: 48rpx;
		border-radius: 0 20rpx 0 20rpx;
	}

	.personal-data .list {
		background-color: #fff;
		padding: 0 30rpx;
	}

	.personal-data .list .item {
		padding: 30rpx 0rpx;
		font-size: 32rpx;
		color: #282828;
		position: relative;
		border-bottom: 1px solid #F5F5F5;
	}
	
	.personal-data .list .item view {
		width: 180rpx;
	}

	.personal-data .list .item .phone {
		width: 160rpx;
		height: 56rpx;
		font-size: 24rpx;
		color: #fff;
		line-height: 56rpx;
		border-radius: 32rpx
	}

	.personal-data .list .item .pictrue {
		width: 88rpx;
		height: 88rpx;
	}

	.personal-data .list .item .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 50%;
	}

	.personal-data .list .item .input {
		width: 75%;
		color: #868686;
		position: absolute;
		top: 50%;
		left: 180rpx;
		transform: translateY(-50%);
		
	}

	.personal-data .list .item .input .id {
		width: 365rpx;
	}

	.personal-data .list .item .input .iconfont {
		font-size: 35rpx;
		margin-right: 20rpx;
	}

	.personal-data .modifyBnt {
		font-size: 32rpx;
		color: #fff;
		width: 690rpx;
		height: 90rpx;
		border-radius: 50rpx;
		text-align: center;
		line-height: 90rpx;
		margin: 76rpx auto 0 auto;
	}

	.personal-data .logOut {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 80rpx;
		font-size: 32rpx;
		text-align: center;
		background-color: #FF4955;
		color: #FFFFFF !important;
	}

</style>
